"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:CheckPaymentAppend
 * @description
 * # CheckPaymentAppend
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller(
    "CheckPaymentAppend",
    function ($scope, $timeout, $routeParams, localStorageService, $location, $rootScope, TaskReviewService, Util, openApi, submitAudit) {
      // 详情接口，审批记录接口，提交审核接口
      var id = ($scope.process_id = $routeParams.id);
      $scope.taskId = id;

      $scope.isShowDeducts = true;
      // 审批操作
      var aduitAction;
      // 新增抄送功能
      $scope.copyto = function () {
        Util.setRelMoNum(1);
        localStorageService.set("chooseUsrs", $scope.aduitUsers);
        localStorageService.set("aduitAction", aduitAction);
        $location.path("chooseuser/index/getFollows");
        $rootScope.$emit("closemasker");
      }
      $scope.auditText = "";
      $scope.check = function (item) {
        if (item.name === "不同意") {
          $scope.hidedisagree = !$scope.hidedisagree
        } else {
          $scope.showAuditContent(item)
        }
      };

      $scope.checkmore = function () {
        $scope.hidedisagree = !$scope.hidedisagree
      };

      $scope.showAuditContent = function (item, $event) {
        if (item.id === 20) {
          // 如果是转交则执行这里的操作
          Util.showFlowTransfer({
            scope: $scope
          })
          $rootScope.$emit("showmasker");
          return
        }
        if (item.id === 30) {
          // 回退的操作
          Util.showFallbackFlow({
            scope: $scope,
          });
          $rootScope.$emit("showmasker");
          return;
        }
        $scope.hidedisagree = true
        $event && $event.stopPropagation()        // 设置蛇皮操作，当页面跳转时候保存起来
        aduitAction = item;
        $rootScope.btnTitle = item.name;//不显示默认值
        $scope.operationId = item.id;
        // 显示审批内容
        Util.showAuditContent({
          scope: $scope
        });
        $scope.showDailog = true;
        $rootScope.$emit("showmasker");
      };
      $scope.$on('closemasker', function () {
        $scope.cancel();
        $rootScope.contenttemplate.remove();
      });
      $scope.confirm = function () {
        $scope.showDailog = false;
        $rootScope.$emit("closemasker");
        completeAudit();
      };
      $scope.cancel = function () {
        console.log("cancel");
        $scope.showDailog = false;
        $rootScope.$emit("closemasker");
        $rootScope.contenttemplate.remove();
      };

      var completeAudit = function () {
        var param = {};
        param.taskId = $scope.TaskReview.result.taskId;
        param.operationId = $scope.operationId;
        param.attachmentDesc = $scope.auditText;
        param.pkId = $scope.TaskReview.result.pkId;
        var deducts = angular.toJson(($scope.TaskReview.result.deductList || []).concat($scope.deductList2));
        param.deducts = deducts
        if ($rootScope.btnTitle === "不同意") {
          param.notPass = 1;
        }
        if ($rootScope.btnTitle === "已阅") {
          param.read = 1;
        }
        // 抄送人
        if ($scope.aduitUsers) {
          param.userIds = $scope.aduitUsers.map(function (item) {
            return item.id
          });
        }
        // 审批附件
        param.attachmentName = $scope.attachmentName || "";
        // 快递单号
        if ($scope.expressNo) {
          param.expressNo = $scope.expressNo;
        }
        param.url = openApi.appendProjectPayment;
        new submitAudit(param);
      };
      // 获取回显的详细信息
      var getDetailData = function (callback) {
        var options = {};
        options.param = {};
        options.param.processInstanceId = id;
        options.param.taskId = id;
        options.url = openApi.getTaskInfo;
        options.callback = callback;
        var TaskReview = new TaskReviewService(options);
        $scope.TaskReview = TaskReview;
      };

      // 选择款项类别
      $scope.costTypeList = [
        {
          key: "保证金",
          value: 3
        },
        {
          key: "押金",
          value: 4
        },
        {
          key: "缴纳",
          value: 5
        },
        {
          key: "成本",
          value: 6
        },
        {
          key: "收入",
          value: 7
        }
      ];
      // 款项类别大类更改时候小类清空
      $scope.costTypechange = function () {
        $scope.picker.deductName = "";
        $scope.getcostsubTypeList();
      };
      $scope.getcostsubTypeList = function () {
        var param = {};
        param.type = "项目款项小类";
        param.parent = $scope.costTypeList.filter(function (item) {
          return item.value == $scope.picker.deductType;
        })[0].key;
        Util.getKeyValue(param, function (data) {
          $scope.costsubTypeList = data.data;
        });
      }
      $scope.deductList2 = $scope.deductList2 || [];

      $scope.showCertificate = function () {
        $rootScope.$emit("showmasker");
        $scope.showCerPicker = true;
      };
      $scope.deletedItem = function (index) {
        $scope.deductList2.splice(index, 1);
        $scope.costindex = -1;
      };
      var closePicker = function () {
        $scope.picker = {};
        $rootScope.$emit("closemasker");
        $scope.showCerPicker = false;
      };
      $scope.$on("closemasker", function () {
        closePicker();
      });
      $scope.swipe = function (index) {
        $scope.costindex = index;
      };
      $scope.resetlist = function () {
        $scope.costindex = -1;
      };
      $scope.addCertificate = function (data) {
        var check = false;
        check = checkPicker(data);
        if (!check) {
          return;
        }
        data.userId  = NativeUtil.getUserInfo().userId;
        $scope.deductList2.push(data);
        // 隐藏表单
        closePicker();
      };
      // 验证是否输入完整
      var checkPicker = function (data) {
        if (!data.deductAmount) {
          Util.Toast("请填写扣款金额");
          return;
        }
        if (!data.deductType) {
          Util.Toast("请选择款项类型");
          return;
        }
        if (!data.deductRemark) {
          Util.Toast("请填写扣款事由");
          return;
        }
        return true;
      };
      // 如果是审批中并且当前登录用户是当前发起人，可以显示提醒一下
      var init = function () {
        $rootScope.nofooter = false
        $scope.showaudit = true;
        $scope.hidedisagree = true
        getDetailData(function (data, formKey) {
          $scope.formKey = formKey;
          // 根据返回数据显示按钮
          Util.showAudit({
            scope: $scope
          });
          var aduitAction = localStorageService.get("aduitAction");
          if (aduitAction) {
            // 用延时，是为了在原来页面被清除之后才初始化渲染
            $timeout(function () {
              // 审批人
              $scope.aduitUsers = localStorageService.get("chooseUsrs");
              if ($scope.aduitUsers) {
                $scope.aduitUserNames = $scope.aduitUsers.map(function (item) {
                  return item.name
                }).join(",");
              }
              localStorageService.remove("aduitAction");
              localStorageService.remove("chooseUsrs");
              $scope.check(aduitAction);
            }, 200)
          }
          $timeout(
            function () {
              Util.insertAduitUsers({
                scope: $scope
              });
              Util.insertRemarkList({
                scope: $scope
              });
            },
            500
          );
        });
      };
      // 初始化
      init();
    }
  );
